<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use \think\Request;
use think\facade\View;
use app\common\model\ProductCate as ProductCateModel;

class ProductCate extends Base
{
	/**
	 * 商品分类列表
	 */
	public function index(Request $request){
		$keyword=trim(input('get.keyword'));
		$cate_id=$request->param("cate_id");
		$whe=array();
		if(!empty($keyword)){
			$whe[]=['title','like','%'.$keyword.'%'];
		}
		
		if(!empty($cate_id)){
			$whe[]=['id','=',$cate_id];
		}
		$whe[]=['status','=','1'];
		
		$whe[]=['pid','=','0'];
		$field = "*";
		$order = "sort asc,id desc";
		$data = ProductCateModel::getList($whe,$field,$order,$this->page);
		$cate = ProductCateModel::getAllList(['pid'=>'0','status'=>'1'],$field,$order);
		foreach($data['list'] as $k=>&$v){
			$w = [];
			$w[] = ['pid','=',$v['id']];
			$w[] = ['status','=',1];
			$v['cate'] = ProductCateModel::getAllList($w,$field,$order);
		}
		View::assign('cate',$cate);
		View::assign('list',$data['list']);
		View::assign('count',$data['count']);
		View::assign('page',$data['page']);
		return View::fetch("ProductCate/index");
	}
	/**
	 * 添加商品分类
	 */
	public function add(Request $request){
		if(!$request->isPost()){
			$w = [];
			$w[] = ['pid','=','0'];
			$w[] = ['status','=','1'];
			$cate = ProductCateModel::getAllList($w);
			View::assign('cate',$cate);
			return View::fetch("ProductCate/add");
		}else{
			$data = input('post.');
			if(empty($data['title'])){
				return $this->renderError("请输入等级名称");
			}
			if(!empty($data['pid'])){
				if(empty($data['imgurl'])){
					return $this->renderError("请上传等级图片");
				}
			}
			$data['status'] = 1;
			$data['addtime'] = time();
			$dd = ProductCateModel::insert($data);
			if($dd){
				return $this->renderSuccess("添加成功");
			}else{
				return $this->renderError("添加失败");
			}

		}
	}
	/**
	 * 编辑商品分类
	 */
	public function edit(Request $request){
		if(!$request->isPost()){
			$id = $request->param('id');
			$data = ProductCateModel::getInfo(['id'=>$id]);
			View::assign('data',$data);
			$w = [];
			$w[] = ['id','<>',$id];
			$w[] = ['pid','=','0'];
			$w[] = ['status','=','1'];
			$cate = ProductCateModel::getAllList($w);
			View::assign('cate',$cate);
			return View::fetch("ProductCate/edit");
		}else{
			$data = input('post.');
			if(empty($data['title'])){
				return $this->renderError("请输入等级名称");
			}
			if(!empty($data['pid'])){
				if(empty($data['imgurl'])){
					return $this->renderError("请上传等级图片");
				}
			}
			$dd = ProductCateModel::where(['id'=>$data['id']])->update($data);
			if($dd){
				return $this->renderSuccess("编辑成功");
			}else{
				return $this->renderError("编辑失败");
			}

		}
	}

	/**
	 * 删除商品分类
	 */
	public function del(Request $request){
		$id=$request->post('id/d');
		$status=$request->post('stu/d');
		if(!in_array($status,array(1,2,3))){
			return $this->renderError("数据错误");
		}
		$data=ProductCateModel::where(['id'=>$id])->field('id')->find();
		if(empty($data)){
			return $this->renderError("数据不存在");
		}
		$res=ProductCateModel::where(['pid'=>$id,'status'=>'1'])->field('id')->find();
		if(!empty($res)){
			return $this->renderError("请先删除该分类下二级类");
		}
		$result = ProductCateModel::where(['id'=>$id])->update(["status"=>$status]);
		if($result){
			return $this->renderSuccess("操作成功");
		}else{
			return $this->renderError("网络繁忙，请稍后");
		}
	}
}